//
//  ss.m
//  TenPeople
//
//  Created by ClassWizard on 9/9/15.
//  Copyright (c) 2015 ClassWizard. All rights reserved.
//

#import "ss.h"
#import "Person.h"
@interface ss()

@property (nonatomic , copy)NSMutableArray *peopleArray;
@end

@implementation ss


- (NSInteger)getNextBeginNumAndCallNumWithBeginNum:(NSInteger)beginNum {
    for (int i = 0; i < _peopleArray.count; i++) {
        Person *person = _peopleArray[i];
        person.callNum = (i+beginNum) % 3;
    }
    NSInteger nextBeginNum = (_peopleArray.count + beginNum) % 3;
    NSLog(@"nextBeginNum:%ld",(long)nextBeginNum);
    return nextBeginNum;
}

- (void)ss {
    //initData
    _peopleArray = [NSMutableArray arrayWithCapacity:10];
    for (int i = 1; i <= 10; i++) {
        Person *person = [[Person alloc] initWithName:[NSString stringWithFormat:@"%d",i]];
        [_peopleArray addObject:person];
    }
    
    NSInteger beginNum = 1;
    while (_peopleArray.count > 1) {
        //1.报数
        beginNum = [self getNextBeginNumAndCallNumWithBeginNum:beginNum];
        //2.将报3的踢出
        for (int i = (int)_peopleArray.count - 1; i >= 0; i--) {
            Person *person = _peopleArray[i];
            if (person.callNum == 0) {
                NSLog(@"删除%@",person.name);
                [_peopleArray removeObject:person];
            }
        }
        NSLog(@"--------------");
    }
    Person *lastPerson = _peopleArray.lastObject;
    NSLog(@"最后剩%@",lastPerson.name);
}
@end
